package com.neusoft.jdmscloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.neusoft.jdmscloud.bean.Role;

@RestController
@RequestMapping("/consumer")
public class RoleConsumerController {
	private Logger logger = LoggerFactory.getLogger(RoleConsumerController.class);
	private static final String REST_URL_PREFIX = "http://JD-MS-CLOUND-PROVIDER";

	/**
	 * 使用 使用restTemplate访问restful接口非常的简单。
	 */
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/role/list")
	@HystrixCommand(fallbackMethod = "processHystrix")
	public List<Role> searchRoles() {
		List<Role> list = new ArrayList<Role>();

		try {
			logger.error("========== RoleConsumerController  9201 ==================");
			// 通过服务名称进行服务调用
			list = restTemplate.getForObject(REST_URL_PREFIX + "/role/list", List.class);
		} catch (RuntimeException e) {
			throw new RuntimeException("没有可用服务");
		}

		return list;
	}
	
	
	public List<Role> processHystrix() {
		
		List<Role> list =  new ArrayList<Role>();
		list.add(new Role(0, "系统没有任何角色"));
		
		return list;
	}
}
